package com.heima.article.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.ArticleFreemarkerService;
import com.heima.common.constants.ArticleConstants;
import com.heima.common.constants.BehaviorConstants;
import com.heima.common.exception.CustomException;
import com.heima.common.redis.CacheService;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.dtos.ArticleInfoDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.article.vos.HotArticleVo;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.article.mess.ArticleVisitStreamMess;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.AppThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Auther: Tangguo
 * @Date: 2023-06-05  17:54
 */
@Service
@Transactional
@Slf4j
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    // 单页最大加载的数字
    private final static short MAX_PAGE_SIZE = 50;
    @Autowired
    private ApArticleMapper apArticleMapper;


    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    @Autowired
    private ArticleFreemarkerService articleFreemarkerService;

    @Autowired
    private CacheService cacheService;

    /**
     * 文章列表加载
     * @param dto:
     * @param type:
     * @return ResponseResult
     */
    @Override
    public ResponseResult load(ArticleHomeDto dto, Short type) {
        //1.参数校验
        if(dto==null&&type==null){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.数据处理
          //2.1如果size前端没传，给个默认值10，如果前端传了，判断是否过大，过大取限定值50
        if(dto.getSize()==null||dto.getSize()<=0){
            dto.setSize(10);
        }else {
            dto.setSize(Math.min(MAX_PAGE_SIZE,dto.getSize()));
        }
          //2.2如果tag没给,设置默认频道"__all__"
        if(StringUtils.isBlank(dto.getTag())) dto.setTag(ArticleConstants.DEFAULT_TAG);
          //2.3如果最大时间和最小时间前端没给，设置当前系统时间
        if(dto.getMaxBehotTime()==null)dto.setMaxBehotTime(new Date());
        if(dto.getMinBehotTime()==null)dto.setMinBehotTime(new Date());
         //2.4如果type没给，默认加载首页，也就是加载更多
        if(type==null) type=ArticleConstants.LOADTYPE_LOAD_MORE;
        //3.业务操作
        List<ApArticle> list = apArticleMapper.loadArticleList(dto, type);

        //4.返回数据

        return ResponseResult.okResult(list);
    }

    /**
     * 加载首页,优先推荐缓存中的热点文章数据
     * 如果缓存中没有，那再去查数据库
     * @param dto
     * @param type
     * @return
     */
    @Override
    public ResponseResult loadFirstPage(ArticleHomeDto dto, Short type) {
        //首页：优先推荐缓存中的热点文章数据
        //如果缓存中没有，那再去查数据库
        String articleJson = cacheService.get(ArticleConstants.HOT_ARTICLE_FIRST_PAGE + dto.getTag());
        if (StringUtils.isNotEmpty(articleJson)){
            List<HotArticleVo> list = JSON.parseObject(articleJson, List.class);
            return ResponseResult.okResult(list);
        }else {
            return load(dto,type);
        }
    }

    /**
     * 保存app端相关文章
     *
     * @param dto :
     * @return ResponseResult
     */
    @Override
    public ResponseResult saveArticle(ArticleDto dto) {
        //1.校验参数
        if(dto==null)return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

        //2.数据处理
        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(dto,apArticle);
        //3.判断是否存在id
        if(dto.getId()==null){
            //3.1 不存在id  保存  文章  文章配置  文章内容
            //保存文章
            save(apArticle);

            //保存配置
            ApArticleConfig apArticleConfig = new ApArticleConfig(apArticle.getId());
            apArticleConfigMapper.insert(apArticleConfig);

            //保存 文章内容
            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(dto.getContent());
            apArticleContentMapper.insert(apArticleContent);

        }else {
            //3.2 存在id   修改  文章  文章内容
            //修改  文章
            updateById(apArticle);

            //修改文章内容
            ApArticleContent apArticleContent = apArticleContentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, dto.getId()));
            apArticleContent.setContent(dto.getContent());
            apArticleContentMapper.updateById(apArticleContent);
        }

        //生成文章详情页
        articleFreemarkerService.buildArticleToMinIO(apArticle,dto.getContent());
        //4.结果返回  文章的id
        return  ResponseResult.okResult(apArticle.getId());
    }

    /**
     * 加载文章详情 数据回显
     * @param dto
     * @return
     */
    @Override
    public ResponseResult loadArticleBehavior(ArticleInfoDto dto) {
        //0.检查参数
        if (dto == null || dto.getArticleId() == null || dto.getAuthorId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //{ "isfollow": true, "islike": true,"isunlike": false,"iscollection": true }
        boolean isfollow = false, islike = false, isunlike = false, iscollection = false;

        ApUser user = AppThreadLocalUtil.getUser();
        if(user != null){
            //喜欢行为
            String likeBehaviorJson = (String) cacheService.hGet(BehaviorConstants.LIKE_BEHAVIOR + dto.getArticleId().toString(), user.getId().toString());
            if(StringUtils.isNotBlank(likeBehaviorJson)){
                islike = true;
            }
            //不喜欢的行为
            String unLikeBehaviorJson = (String) cacheService.hGet(BehaviorConstants.UN_LIKE_BEHAVIOR + dto.getArticleId().toString(), user.getId().toString());
            if(StringUtils.isNotBlank(unLikeBehaviorJson)){
                isunlike = true;
            }
            //是否收藏
            String collctionJson = (String) cacheService.hGet(BehaviorConstants.COLLECTION_BEHAVIOR+user.getId(),dto.getArticleId().toString());
            if(StringUtils.isNotBlank(collctionJson)){
                iscollection = true;
            }

            //是否关注
            Double score = cacheService.zScore(BehaviorConstants.APUSER_FOLLOW_RELATION + user.getId(), dto.getAuthorId().toString());
            System.out.println(score);
            if(score != null){
                isfollow = true;
            }

        }

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("isfollow", isfollow);
        resultMap.put("islike", islike);
        resultMap.put("isunlike", isunlike);
        resultMap.put("iscollection", iscollection);

        return ResponseResult.okResult(resultMap);

    }

    /**
     * 更新热点文章的分值及修改文章行为数据  同时更新缓存中的热点文章数据
     * @param mess :
     * @return void
     */
    @Override
    public void updateScore(ArticleVisitStreamMess mess) {
        //1.先拿到更新前的分数值
        ApArticle apArticle = apArticleMapper.selectById(mess.getArticleId());
        Integer oldScore=computeScore(apArticle);

        //2.更新数据库中文章的行为数据
        ApArticle article=updateArticle(mess);

        //3.对当前文章重新算分
        article.setCollection(mess.getCollect());
        article.setComment(mess.getComment());
        article.setLikes(mess.getLike());
        article.setViews(mess.getView());
        Integer scoreToday=computeScore(apArticle);
        scoreToday *= 3;        //当日热度权重整体 * 3

        Integer score = oldScore + scoreToday;
        //4.更新对应频道的热点文章
        replaceDataToRedis(article,score,ArticleConstants.HOT_ARTICLE_FIRST_PAGE+apArticle.getChannelId());
        //5.更新推荐频道的热点文章
        replaceDataToRedis(article,score,ArticleConstants.HOT_ARTICLE_FIRST_PAGE+ArticleConstants.DEFAULT_TAG);



    }

    /**
     * 替换数据并且存入到redis
     * @param apArticle
     * @param score
     * @param channelId
     */
    private void replaceDataToRedis(ApArticle apArticle, Integer score, String channelId) {
        //1.根据频道id查询redis文章数据
        String articleListStr= cacheService.get(channelId);
        //2.校验数据是否为空
        if(StringUtils.isNotBlank(articleListStr)){
            List<HotArticleVo> hotArticleVoList = JSON.parseArray(articleListStr, HotArticleVo.class);
            boolean flag=true;

            //2.1.如果缓存中存在该文章，只更新分值
            for (HotArticleVo hotArticleVo : hotArticleVoList) {
                if(hotArticleVo.getId().equals(apArticle.getId())){
                    hotArticleVo.setScore(score);
                    flag=false;
                    break;
                }
            }
            //2.2.如果缓存中不存在，查询缓存中分值最小的一条数据，进行分值的比较，如果当前文章的分值大于缓存中的数据，就替换
            if(flag){
                if(hotArticleVoList.size()>=30){
                    hotArticleVoList= hotArticleVoList.stream().sorted(Comparator.comparing(HotArticleVo::getScore).reversed()).collect(Collectors.toList());
                    //获取最后一篇热点文章
                    HotArticleVo lastHotArticle = hotArticleVoList.get(hotArticleVoList.size() - 1);
                    if(lastHotArticle.getScore()<score){
                        hotArticleVoList.remove(lastHotArticle);
                        HotArticleVo hot =new HotArticleVo();
                        BeanUtils.copyProperties(apArticle,hot);
                        hot.setScore(score);
                        hotArticleVoList.add(hot);
                    }else {
                        HotArticleVo hot =new HotArticleVo();
                        BeanUtils.copyProperties(apArticle,hot);
                        hot.setScore(score);
                        hotArticleVoList.add(hot);
                    }

                }
                //3.缓存到redis
                hotArticleVoList=hotArticleVoList.stream().sorted(Comparator.comparing(HotArticleVo::getScore).reversed()).collect(Collectors.toList());
                cacheService.set(channelId,JSON.toJSONString(hotArticleVoList));
            }
        }


    }

    /**
     * 更新文章行为数量
     * @param mess
     */
    private ApArticle updateArticle(ArticleVisitStreamMess mess) {
        ApArticle apArticle=getById(mess.getArticleId());
        apArticle.setCollection((apArticle.getCollection()==null?0:apArticle.getCollection())+mess.getCollect());
        apArticle.setComment((apArticle.getComment()==null?0:apArticle.getComment())+mess.getComment());
        apArticle.setLikes((apArticle.getLikes()==null?0:apArticle.getLikes())+mess.getLike());
        apArticle.setViews((apArticle.getViews()==null?0:apArticle.getViews())+mess.getView());
        updateById(apArticle);
        return apArticle;
    }

    /**
     * 计算文章的具体分值
     * @param apArticle
     * @return
     */
    private Integer computeScore(ApArticle apArticle) {
        Integer score = 0;
        if(apArticle.getLikes() != null){
            score += apArticle.getLikes() * ArticleConstants.HOT_ARTICLE_LIKE_WEIGHT;
        }
        if(apArticle.getViews() != null){
            score += apArticle.getViews();
        }
        if(apArticle.getComment() != null){
            score += apArticle.getComment() * ArticleConstants.HOT_ARTICLE_COMMENT_WEIGHT;
        }
        if(apArticle.getCollection() != null){
            score += apArticle.getCollection() * ArticleConstants.HOT_ARTICLE_COLLECTION_WEIGHT;
        }
        return score;
    }
}
